#!/usr/bin/env perl
#
#
#
# EM4100 decoded: 0D 00 3B 07 A7
# EM4100        : FF 83 60 01  
#                 AE 07 D1 FE
# Byte track    : FF 7F C1 B0
#                 00 D7 03 E8
#
# EM4100 decoded: 10 00 50 98 CA
# EM4100        : FF 8C 00 02
#                 81 28 E2 86
# Byte track    : FF 8C 00 02
#                 81 28 E2 86
#
#
#

use strict;
use warnings;
use bytes;

use Device::SerialPort;
use Time::Hires qw(sleep);

my $PORT = "/dev/tty.usbserial";

my $ob;
$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate(19200)   || die "failed setting baudrate";
$ob->parity("none")    || die "failed setting parity";
$ob->databits(8)       || die "failed setting databits";
$ob->handshake("none") || die "failed setting handshake";
$ob->read_const_time(1000) || die "failed setting read timeout"; # 1 sec
$ob->read_char_time(20) || die "failed setting read timeout"; 
$ob->write_settings    || die "no settings";

print "port open\n";

my $wrcnt;
my ($count_resp, $response);
my $cmd_count = 8;
my $id_count = 10;

my $cmd_read = "re364\x02\x01\x06";
#my $cmd_read = 're364' . 0x02 . 0x01 . 0x07;
my $cmd_ack  = 'acknwlge';
my $cmd_ack2 = 'acknokok';
my $resp_reset = 'resetSP1';
my $send_cmd = 1;
my $tag_id = 0;
my $tag_reads = 0;

$ob->read(100);  # drain

while( 1 ) {
    if( $send_cmd ) {
        $wrcnt = $ob->write($cmd_read);                    # send command
        print "sent cmd0:$cmd_read, wrcnt:$wrcnt\n";

        ($count_resp, $response) = $ob->read($cmd_count);  # get response
        print "response:$response  ($count_resp)\n";
        if( $response ne $cmd_read ) {
            print "error: echo-back doesn't match: '$response'\n";
        }

        $wrcnt = $ob->write($cmd_ack);                     # send ack
        print "sent cmd1:$cmd_ack, wrcnt:$wrcnt\n";

        ($count_resp, $response) = $ob->read($cmd_count);  # get ack
        print "response:$response  ($count_resp)\n";
        if( $response ne $cmd_ack ) {
            print "error: did not acknowledge correctly: '$response'\n";
        }
        $wrcnt = $ob->write($cmd_ack2);                    # send ack2
        print "send cmd2:$cmd_ack2\n";

        ($count_resp, $response) = $ob->read($cmd_count);  # get ack2
        print "response:$response  ($count_resp)\n";
        if( $response ne $cmd_ack2 ) {
            print "error: did not acknowledge correctly: '$response'\n";
        }

        $send_cmd = 0;
        $tag_id = 0;
    }

    ($count_resp, $response) = $ob->read($id_count);
    print "response:$response  ($count_resp)\n";
    if( $count_resp > 0 ) {   # we received something
        if( $count_resp == 10 ) {
            $tag_id = $response;
            $send_cmd = 1;
            sleep(1);
        }
        if( $response eq $resp_reset ) {
            $send_cmd = 1;
            sleep(1);
        }
    }

    if( $tag_id ) {
        # consume it
        print "\nTag ID: $tag_id  ($tag_reads)\n\n";
        $tag_id = 0;
    }
    sleep 0.02;
}
